package com.bw.mvvm_common.mvvm.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.blankj.utilcode.util.LogUtils;
import com.bw.mvvm_common.BaseApplication;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/14 0014 下午 3:08
 *
 */
abstract class BaseFragment<V extends ViewDataBinding, VM extends BaseViewModel> extends Fragment
        implements IView{
    protected V v;
    protected VM vm;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        bindDataBinding();
        v.setVariable(initVerable(),vm);
        return v.getRoot();
    }

    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        getLifecycle().addObserver(vm);
        vm.getUiChange().getHideDialogObservable().observe(this,  hideDialogObserver);
        vm.getUiChange().getFinishActObservable().observe(this,finishObserver);
        vm.getUiChange().getShowDialogObservable().observe(this,showDialogObserver);
        vm.getUiChange().getShowMsgObservable().observe(this,showMsgObserver);
        vm.getUiChange().getStartPageObservable().observe(this,startPageObserver);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        vm.getUiChange().getHideDialogObservable().removeObserver(hideDialogObserver);
        vm.getUiChange().getFinishActObservable().removeObserver(finishObserver);
        vm.getUiChange().getShowDialogObservable().removeObserver(showDialogObserver);
        vm.getUiChange().getShowMsgObservable().removeObserver(showMsgObserver);
        vm.getUiChange().getStartPageObservable().removeObserver(startPageObserver);
        getLifecycle().removeObserver(vm);
        v.unbind();
    }

    @Override
    public void showDialog() {
        LogUtils.e("showDialog");
    }

    @Override
    public void hideDialog() {
        LogUtils.e("hideDialog");
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(BaseApplication.getApplication(), msg, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void finishpage() {
        getActivity().finish();
    }

    @Override
    public void startPage(Class clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (bundle!=null){
            intent.putExtra(UIField.PAGEBUNDLE,bundle);
        }
        startActivity(intent);
    }



    @Override
    public void initViewModel() {
        ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
        Type[] types = parameterizedType.getActualTypeArguments();
        Class clazz = (Class) types[1];
        vm= (VM) new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(clazz);
    }

    @Override
    public void bindDataBinding() {
        v = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
                bindLayout(),null,false);
    }


    private Observer<String> showDialogObserver = new Observer<String>() {
        @Override
        public void onChanged(String s) {
            showDialog();
        }
    };
    private Observer<String>hideDialogObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            hideDialog();
        }
    };
    private Observer<String>finishObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            finishpage();
        }
    };
    private Observer<String>showMsgObserver = new Observer<String>(){
        @Override
        public void onChanged( String o) {
            showMsg(o);
        }
    };
    private Observer<HashMap<String,Object>>startPageObserver = new Observer<HashMap<String, Object>>() {
        @Override
        public void onChanged( HashMap<String, Object> map) {
            Class clazz = (Class) map.get(UIField.PAGECLAZZ);
            Object o = map.get(UIField.PAGEBUNDLE);
            Bundle bundle = null;
            if (o!=null)
                bundle = (Bundle) o;
            startPage(clazz,bundle);
        }
    };
}
